//
//  StatusListViewModel.swift
//  weibo
//
//  Created by mac on 15/12/20.
//  Copyright © 2015年 rong. All rights reserved.
//

import UIKit
import SVProgressHUD
class StatusListViewModel: NSObject {

    class func loadHomePageData(finished:(statuses:[Status]?) ->()) {
        //实现网络请求
        //get请求
        let urlString = "2/statuses/home_timeline.json"
        //判断token是否为空
        guard let token = UserAccountViewModel().token else {
        print("token为空")
            SVProgressHUD.showInfoWithStatus("请重新登录")
            return
        }
        let parameters = ["access_token" : token]
        
        NetworkTools.sharedTools.requestJSONDict(.GET, urlString: urlString, parameters: parameters) { (dict, error) -> () in
            if error != nil {
            //执行失败的回调
                SVProgressHUD.showInfoWithStatus("你的网络正在睡觉,请傻等")
                finished(statuses: nil)
                return
            }
            //请求成功
            if let array = dict!["statuses"] as? [[String:AnyObject]] {
            //遍历数组中所有的字典 做字典转模型的操作
                var list = [Status]()
                for item in array {
                let s = Status(dict: item)
                    list.append(s)
                }
                //执行成功的回调
                finished(statuses: list)
            }
        }
    }
}
